/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.help.web;

import com.googlecode.genericdao.search.Filter;
import com.googlecode.genericdao.search.Search;
import java.util.ArrayList;
import org.crystalrange.help.model.HelpFolder;
import org.crystalrange.help.model.HelpItem;
import org.crystalrange.help.service.HelpFolderService;
import org.crystalrange.help.service.HelpItemService;

public class HelpFolderTreeNode extends TreeNode {
    
    private Long id = null;
    private String name = "";
    private String description = "";
    private String location = "";
    private HelpFolderService helpFolderService;
    private HelpItemService helpItemService;

    public void setId(Long id) {
        this.id = id;
    }

    public Long getId() {
        return id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDescription() {
        return this.description;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getLocation() {
        return this.location;
    }

    public HelpFolderService getHelpFolderService() {
        return this.helpFolderService;
    }

    public void setHelpFolderService(HelpFolderService helpFolderService) {
        this.helpFolderService = helpFolderService;
    }

    public HelpItemService getHelpItemService() {
        return this.helpItemService;
    }

    public void setHelpItemService(HelpItemService helpItemService) {
        this.helpItemService = helpItemService;
    }

    @Override
    public void readChildren() {
        this.children = new ArrayList<TreeNode>();

        Search searchHelpFolder = new Search(HelpFolder.class);
        searchHelpFolder.addFilterSome("parentHelpFolder", Filter.equal("id", this.id));

        for (HelpFolder helpFolder : this.helpFolderService.search(searchHelpFolder)) {
            HelpFolderTreeNode helpFolderTreeNode = new HelpFolderTreeNode();
            helpFolderTreeNode.setHelpFolderService(this.helpFolderService);
            helpFolderTreeNode.setHelpItemService(this.helpItemService);
            helpFolderTreeNode.setId(helpFolder.getId());
            helpFolderTreeNode.setName(helpFolder.getName());
            helpFolderTreeNode.setDescription(helpFolder.getDescription());
            helpFolderTreeNode.setLocation(helpFolder.getLocation());            
            this.children.add(helpFolderTreeNode);
        }

        Search searchHelpItem = new Search(HelpItem.class);
        searchHelpItem.addFilterSome("parentHelpFolder", Filter.equal("id", this.id));

        for (HelpItem helpItem : this.helpItemService.search(searchHelpItem)) {
            HelpItemTreeNode helpItemTreeNode = new HelpItemTreeNode();
            helpItemTreeNode.setHelpFolderService(this.helpFolderService);
            helpItemTreeNode.setHelpItemService(this.helpItemService);
            helpItemTreeNode.setId(helpItem.getId());
            helpItemTreeNode.setName(helpItem.getName());
            helpItemTreeNode.setDescription(helpItem.getDescription());
            helpItemTreeNode.setLocation(helpItem.getLocation());
            this.children.add(helpItemTreeNode);
        }
    }

    @Override
    public boolean isLeaf() {
        return false;
    }
}